﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Talentnet.Core.Web;
using Talentnet.Data.Entity;
using Talentnet.Core.Utilities.Mail;

[PageTextResource("ForgotPwd")]
public partial class forgotPwd : NormalPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ctrlCaptcha.CaptchaLinkButtonText = LoadText("Login", "ctrlCaptcha");
        ctrlCaptcha.ErrorMessage = LoadText("Login", "CaptchaErrorMessage");
        ctrlMsg.Visible = false;
    }
    protected void ctrlOK_Click(object sender, EventArgs e)
    {
        //if (ctrlCaptcha.Visible && !ctrlCaptcha.IsValid)
        //{
        //    return;
        //}
        LoginUser usr = this.DBUser.GetUserDetail(inLoginName.Text);
        if (usr == null)
        {
            ctrlMsg.Visible = true;
            return;
        }
        if (inEmail.Text.ToLower().Trim() != usr.Email.ToLower().Trim())
        {
            ctrlMsg.Visible = true;
            return;
        }
        string validKey = Talentnet.Core.Utilities.Cryptophy.CryptoAgent.Current.MD5(DateTime.Now.ToString());
        DBUser.CreateForgotPwdRequest(usr.RecId, validKey);

        string requestUrl = SiteConstants.System.ROOTURL + "login.aspx?" + BuildStaticQueryString(string.Format("uid={0}&key={1}", usr.RecId, validKey));
        //send email here
        string fromEmail = SessionManager.MailSetting.EmailContact;
        //Load mail template
        System.IO.StreamReader mailTemplate = new System.IO.StreamReader(MapPath(@"~\App_Data\EmailTemplates\ForgotPwd.htm"));
        string mail_body = mailTemplate.ReadToEnd();
        string subject = "[Talentnet] - Quen Mat Khau";
        mailTemplate.Close();
        mail_body = mail_body.Replace("[username]", usr.LoginName);

        mail_body = mail_body.Replace("[link]", requestUrl);
        MailServices.SendEmail(fromEmail, usr.Email, subject, mail_body, SessionManager.MailSetting);
        Response.Redirect("~/freeaccess/message.aspx?" + BuildSecureQueryString("mode=ForgotPwd"));
    }
}
